package com.yc.web.servlets;

import com.yc.utils.EncryptUtils;
import com.yc.web.model.JsonModel;
import com.yc.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import static com.yc.utils.YcConstants.USER;


@WebServlet(name = "UserServlet" ,value="/user.action")
public class UserServlet_zhou extends BaseServlet {
    //登录
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        JsonModel jm = new JsonModel();
        User c=super.parseObjectFromRequest(req, User.class);
        String valcode=req.getParameter("valcode");
        //验证码开关   ture：验证码功能开启     false:验证码功能关闭
        boolean validate = false;
        //取出session的验证码
        HttpSession session=req.getSession();
        if ( validate) {
            String code = (String) session.getAttribute("code");
            if (validate && !code.equals(valcode)) {
                jm.setCode(0);
                jm.setError("验证码错误...");
                super.writeJson(jm, resp);
                return;
            }
        }
        String newPwd= EncryptUtils.encrytToMD5(EncryptUtils.encrytToMD5(c.getPwd()));
        c.setPwd(newPwd);
        String sql="select * from user where username=? and pwd=?";
        List<User> list = db.select(User.class,sql,c.getUsername(),c.getPwd());
        if (list!=null&&list.size()>0){
            User rs = list.get(0);
            rs.setPwd("别偷看.....");
            jm.setCode(1);
            jm.setObj(list.get(0));

            //记录用户的登录状态
            session.setAttribute(USER,list.get(0));
            //访问redis

        }else{
            jm.setCode(0);
            jm.setError("用户名或密码错误");
        }
        super.writeJson(jm,resp);
    }
    //登录退出
    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, InstantiationException, IllegalAccessException {
        HttpSession session = req.getSession();
        session.removeAttribute(USER);
        JsonModel jm=new JsonModel();
        jm.setCode(1);
        super.writeJson(jm,resp);
    }
    //登录状态保持
    protected void checkLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, InstantiationException, IllegalAccessException {
        HttpSession session = req.getSession();
        JsonModel jm = new JsonModel();
        if (session.getAttribute(USER)!=null){
            jm.setCode(1);
            User rs = (User) session.getAttribute(USER);
            rs.setPwd("别偷看...");
            jm.setObj(rs);
        }else {
            jm.setCode(0);
        }
        super.writeJson(jm,resp);
    }
    //注册登录
    protected void reg(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        JsonModel jm=new JsonModel();
        User c=super.parseObjectFromRequest(req, User.class);
        String username =req.getParameter("username");
        String pwd=req.getParameter("pwd");
        String email = req.getParameter("email");

        //取出session中的验证码
        HttpSession session=req.getSession();
        String newPwd= EncryptUtils.encrytToMD5(EncryptUtils.encrytToMD5(c.getPwd()));
        c.setPwd(newPwd);
        String sql="select * from user where username=?";
        List<User> test = db.select(User.class,sql,c.getUsername());
        if (test!=null&&test.size()>0){
            jm.setCode(0);
            jm.setError("用户名重复");
            super.writeJson(jm,resp);
        }else{
            String sql1 = "insert into user(username, pwd,email) values(?, ?, ?)";
            int list=db.doUpdate(sql1,c.getUsername(),c.getPwd(),c.getEmail());

            if (list>=1){
                //记录用户的登录状态
                jm.setCode(1);
            }else{
                jm.setCode(0);
                jm.setError("系统内部原因");
            }
            super.writeJson(jm,resp);
        }

    }
}
